package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrSequenceOf;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.nio.ByteBuffer;

public class ProxyGetResponseList extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piidAcd = null;
    private SubSeqOfResult result = null;

    public ProxyGetResponseList() {
    }

    public ByteBuffer getDataCode() {
        return dataCode;
    }

    public PIID getPiidAcd() {
        return piidAcd;
    }

    public SubSeqOfResult getResult() {
        return result;
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid_acd, SubSeqOfResult result) {
        this.piidAcd = piid_acd;
        this.result = result;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piidAcd = new PIID();
        this.piidAcd.setNumBit(8);
        codeLength += this.piidAcd.decode(input);
        this.result = new SubSeqOfResult();
        codeLength += this.result.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.result != null && this.piidAcd != null) {
                codeLength += this.result.encode(output);
                codeLength += this.piidAcd.encode(output);
            } else {
                throw new RuntimeException("piid_acd || result is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.piidAcd != null && this.result != null) {
            return "sequence: {" + "piid_acd: " + this.piidAcd + ", result: " + this.result + "}";
        } else {
            return "piid_acd || result is null";
        }
    }
}



